import hashlib
import binascii
import base58

def genWIF(hex_str):
    # 16进制私钥
    # hex_str = "bb82deb2e63ba0eced75004235813ad2b82dd3a9b989e075e90f87b480b58bf6"
    # 在16进制私钥前面加上0x80版本号，如下：
    hex_string = "80"+hex_str
    # hex_k = bytes.fromhex(hex_string).hex()
    hex_k1 = bytes.fromhex(hex_string)
    # 对第1步结果进行SHA256哈希计算，如下：hex_k2:f7d98762db7267f6f9283156058333dce856036ae0a0b4741046d25058747739
    hex_k2 = hashlib.sha256(hex_k1).hexdigest()
    # 将第2步结果进行SHA256哈希计算，如下：hex_k3:c47e83ffafda3ba4396e1bc6a648515e5fc9aa95910af6a4429537b87fb7b474
    hex_k3b = bytes.fromhex(hex_k2)
    hex_k3 = hashlib.sha256(hex_k3b).hexdigest()
    # 取第3步结果的前4字节(c47e83ff)，加到第1步结果的末尾，如下：hex_k4:801e99423a4ed27608a15a2616a2b0e9e52ced330ac530edcc32c8ffc6a526aeddc47e83ff
    hex_k4 = hex_string + hex_k3[0:8]
    # 对第4步结果进行Base58编码，就是最后得到的WIF格式私钥了，如下：base58_str:5J3mBbAH58CpQ3Y5RNJpUKPE62SQ5tfcvU2JpbnkeyhfsYB1Jcn
    # 将16进制字符串转换为二进制
    k5 = binascii.unhexlify(hex_k4)
    # 转换为Base58
    base58_str = base58.b58encode(k5)
    print(base58_str.decode('utf-8'))
    return base58_str.decode('utf-8')